package com.hermes.action.login;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.ParameterParserUtil;
import com.hermes.utils.logger.LoggerUtil;

/**
* 
* 用户登录的跳转
* 
*/

public class LoginAction extends BaseAction {

    private static final long   serialVersionUID = -2841153430630242279L;

    private static final Logger LOGGER           = Logger.getLogger(LoginAction.class);

    private UserService         userService;

    private String              name;

    public String execute() {

        String redirectUrl = ParameterParserUtil.getParameter("redirectUrl");
        LoggerUtil.error(LOGGER, "------------", redirectUrl);
        //        String username = ParameterParserUtil.getParameter("username");
        //        String password = ParameterParserUtil.getParameter("password");
        String username = ParameterParserUtil.getParameter("UserName");
        String password = ParameterParserUtil.getParameter("UserPwd");
        LoggerUtil.error(LOGGER, password, username);
        UserResultDO userResultDO = userService.getUserInfoByUserName(username);

        if (userResultDO.isSuccess()) {
            if (userResultDO.getModule() != null
                && userResultDO.getModule().getUserPassword().endsWith(password)) {
                setName(username);
                session.put("userName", username);
                session.put("userId", userResultDO.getModule().getUserId());
                HttpServletResponse response = ServletActionContext.getResponse();
                try {
                    redirectUrl = URLDecoder.decode(redirectUrl, "GBK");
                    response.sendRedirect(redirectUrl);
                } catch (IOException e) {
                    LoggerUtil.error(LOGGER, e);
                }

                return SUCCESS;
            }
        }
        return ERROR;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}